//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.view
{
	import __AS3__.vec.Vector;
	
	import flash.events.Event;
	import flash.utils.Timer;
	
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	import reader.ApplicationFacade;
	import reader.core.IGCAble;
	import reader.core.ReaderModelLocator;
	import reader.model.IDraftProxy;
	import reader.model.IServiceProxy;
	import reader.services.IPostBlogAble;
	import reader.view.components.BlogWriter;
	
	public class BlogWriterMediator extends ServiceAbleMediator 
											implements IMediator, IGCAble
	{
		////////////////////////////////////////////////////////////////////////////////
		//public properties
		
		public static const NAME:String = "BlogWriterMediator";
		
		public function get blogWriter():BlogWriter
		{
			return this.viewComponent as BlogWriter;
		}
		
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		private const SAVE_INTERVAL:uint = 1000;
		
		private var draftProxy:IDraftProxy;
		private var serviceProxy:IServiceProxy;
		
		private var saveTimer:Timer;
		private var saveDownCounter:uint = 60;
		private var isSaveTimerOn:Boolean;
		private var autoSaving:String;
		private var autoSaved:String;
		
		private var postBlogService:IPostBlogAble;
		
		private var readerModelLocator:ReaderModelLocator = ReaderModelLocator.instance;
		
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function BlogWriterMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			
			startUp();	
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
		
		override public function startUp():void
		{
			draftProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.draftProxyName) as IDraftProxy;
			serviceProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.serviceProxyName) as IServiceProxy;
			
			blogWriter.addEventListener(BlogWriter.CLOSE_TYPED_WINDOW, onCloseWindow_handler);
			blogWriter.managerViewStack.addEventListener(Event.CHANGE, onChangeStack_handler);
			
			facade.registerMediator(new DashboardMediator(blogWriter.dashBoard));
			facade.registerMediator(new WriterMediator(blogWriter.newBlogView));
			facade.registerMediator(new EditorMediator(blogWriter.editBlogView));
			facade.registerMediator(new MediaManagerMediator(blogWriter.mediaManageView));
			
			sendNotification(ApplicationFacade.VIEW_START_UP, blogWriter.initView);
		}
		
		override public function clearReference():void
		{
			blogWriter.managerViewStack.removeEventListener(Event.CHANGE, onChangeStack_handler);
			draftProxy = null;
			serviceProxy = null;
			readerModelLocator = null;
			sendNotification(ApplicationFacade.WRITER_CLOSE);
		}
				
		override public function listNotificationInterests():Vector.<String>
		{
			return Vector.<String>([ApplicationFacade.EDIT_DRAFT]);
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case ApplicationFacade.EDIT_DRAFT:
					blogWriter.setViewStack(BlogWriter.VIEW_NEW_BLOG);
					break;
			}
		}

		
		//////////////////////////////////////////////////////////////////////////////
		//events
		
		private function onCloseWindow_handler(e:Event):void
		{
			blogWriter.removeEventListener(BlogWriter.CLOSE_TYPED_WINDOW, onCloseWindow_handler);
			clearReference();
		}
		
		private function onChangeStack_handler(e:Event):void
		{
			sendNotification(ApplicationFacade.WRITER_VIEW_CHANGE, blogWriter.getViewStackName(blogWriter.managerViewStack.selectedIndex));
		}
		
	}
}